package com.kl.last;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import org.junit.Test;

public class FinallyTest {

    private static final String XML = "<xml>\n" +
            "    <ToUserName><![CDATA[toUser]]></ToUserName>\n" +
            "    <FromUserName><![CDATA[sys]]></FromUserName>\n" +
            "    <CreateTime>1403610513</CreateTime>\n" +
            "    <MsgType><![CDATA[event]]></MsgType>\n" +
            "    <Event><![CDATA[change_contact]]></Event>\n" +
            "    <ChangeType>create_user</ChangeType>\n" +
            "    <UserID><![CDATA[zhangsan]]></UserID>\n" +
            "    <Name><![CDATA[张三]]></Name>\n" +
            "    <Department><![CDATA[1,2,3]]></Department>\n" +
            "    <MainDepartment>1</MainDepartment>\n" +
            "    <IsLeaderInDept><![CDATA[1,0,0]]></IsLeaderInDept>\n" +
            "    <Position><![CDATA[产品经理]]></Position>\n" +
            "    <Mobile>13800000000</Mobile>\n" +
            "    <Gender>1</Gender>\n" +
            "    <Email><![CDATA[zhangsan@gzdev.com]]></Email>\n" +
            "    <Status>1</Status>\n" +
            "    <Avatar>\n" +
            "        <![CDATA[http://wx.qlogo.cn/mmopen/ajNVdqHZLLA3WJ6DSZUfiakYe37PKnQhBIeOQBO4czqrnZDS79FH5Wm5m4X69TBicnHFlhiafvDwklOpZeXYQQ2icg/0]]></Avatar>\n" +
            "    <Alias><![CDATA[zhangsan]]></Alias>\n" +
            "    <Telephone><![CDATA[020-123456]]></Telephone>\n" +
            "    <Address><![CDATA[广州市]]></Address>\n" +
            "    <ExtAttr>\n" +
            "        <Item>\n" +
            "            <Name><![CDATA[爱好]]></Name>\n" +
            "            <Type>0</Type>\n" +
            "            <Text>\n" +
            "                <Value><![CDATA[旅游]]></Value>\n" +
            "            </Text>\n" +
            "        </Item>\n" +
            "        <Item>\n" +
            "            <Name><![CDATA[卡号]]></Name>\n" +
            "            <Type>1</Type>\n" +
            "            <Web>\n" +
            "                <Title><![CDATA[企业微信]]></Title>\n" +
            "                <Url><![CDATA[https://work.weixin.qq.com]]></Url>\n" +
            "            </Web>\n" +
            "        </Item>\n" +
            "    </ExtAttr>\n" +
            "</xml>\n";

    @Test
    public void test() throws JsonProcessingException {
        XmlMapper xmlMapper = new XmlMapper();
        //字段为null，自动忽略，不再序列化
        xmlMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        xmlMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,false);
        CreateUserEvent userEvent = xmlMapper.readValue(XML, CreateUserEvent.class);
        System.out.println(userEvent);
    }
}
